<template>
	<view class="home-layout pageBg">
		<!-- 自定义导航栏 -->
		<CustomNavBar title="推荐"></CustomNavBar>
		
		<!-- 轮播图 -->
		<view class="banner">
			<swiper 
				class="my-swiper"
				indicator-dots
				indicator-color="rgba(255,255,255,0.5)"
				indicator-active-color="#fff"
				autoplay
				circular
			>
				<swiper-item
				  class="my-swiper-item"
				  v-for="item in bannerList"
					:key="item._id"
				>
					<image :src="item.picurl" mode="aspectFill"></image>
				</swiper-item>
			</swiper>
		</view>
		
		<!-- 公告（竖向轮播图） -->
		<view class="notice">
			<view class="left">
				<uni-icons type="sound-filled" size="20"></uni-icons>
				<text class="text">公告</text>
			</view>
			<view class="center">
				<swiper 
					class="my-swiper"
					vertical
					autoplay
					interval="1500"
					duration="300"
					circular
				>
					<swiper-item class="my-swiper-item" v-for="item in noticeList" :key="item._id">
						<navigator :url="'/pages/detail/detail?id='+item._id">
							{{item.title}}
						</navigator>
					</swiper-item>
				</swiper>
			</view>
			<view class="right">
				<uni-icons type="right" size="20" color="#333"></uni-icons>
			</view>
		</view>
		
		<!-- 每日推荐 -->
		<view class="select">
			<commonTitle>
				<!-- 具名插槽 -->
				<template #name>每日推荐</template>
				<template #custom>
					<view class="date">
						<uni-icons type="calendar" size="18"></uni-icons>
						<!-- 日期格式化 -->
						<view class="text">
							<uni-dateformat :date="Date.now()" format="yyyy年MM月dd日"></uni-dateformat>
						</view>
					</view>
				</template>
			</commonTitle>
			
			<view class="content">
				<!-- 横向滑动 -->
				<scroll-view scroll-x>
					<view class="box" v-for="item in randomList" :key="item._id">
						<image :src="item.smallPicurl" mode="aspectFill"></image>
					</view>
				</scroll-view>
			</view>
		</view>
	
		<!-- 专题精选 -->
		<view class="theme">
			<CommonTitle>
				<template #name>专题精选</template>
				<template #custom>
					<navigator url="" class="more">More+</navigator>
				</template>
			</CommonTitle>
			<view class="content">
				<ThemeItem 
					v-for="item in classifyList" 
					:key="item._id"
					:item="item"
				>
				</ThemeItem>
				<ThemeItem :isMore="true"></ThemeItem>
			</view>
		</view>
		
	</view>
</template>

<script setup>
import CommonTitle from '@/components/common-title/common-title';
import ThemeItem from '@/components/theme-item/theme-item';
import CustomNavBar from '@/components/custom-nav-bar/custom-nav-bar';

import { onReady,onShareAppMessage,onShareTimeline } from "@dcloudio/uni-app";
import { ref } from 'vue';
import { apiGetBanner,apiGetDayRandom,apiGetNotice,apiGetClassify } from '@/api/api';

// 轮播图
const bannerList = ref([])
// 获取轮播图
const getBanner = async () => {
	let res = await apiGetBanner()
	bannerList.value = res.data
}

// 随机9张壁纸
const randomList = ref([])
const getDayRandom = async () => {
	let res = await apiGetDayRandom()
	randomList.value = res.data
}

// 公告
const noticeList = ref([])
const getNotice = async () => {
	let res = await apiGetNotice({
		select: true
	})
	noticeList.value = res.data
}

// 专题精选图片
const classifyList = ref([])
const getClassify = async () => {
	let res = await apiGetClassify({
		// pageNum: 1,
		// pageSize: 10,
		select: true
	})
	classifyList.value = res.data
}

onReady(() => {
	getBanner()
	getDayRandom()
	getNotice()
	getClassify()
})

//分享给好友
onShareAppMessage((e)=>{
	return {
		title:"咸虾米壁纸，好看的手机壁纸",
		path:"/pages/classify/classify"
	}
})
//分享朋友圈
onShareTimeline(()=>{
	return {
		title:"咸虾米壁纸，好看的手机壁纸"
	}
})
</script>

<style lang="scss" scoped>
.home-layout{
	
	// 轮播图
	.banner{
		width: 750rpx;
		padding: 30rpx 0;
		
		.my-swiper{
			width: 750rpx;
			height: 340rpx;
			
			.my-swiper-item{
				width: 100%;
				height: 100%;
				padding: 0 30rpx;
				
				image{
					width: 100%;
					height: 100%;
					border-radius: 10rpx;
				}
			}
		}
		
	}

	// 公告
	.notice{
		width: 690rpx;
		height: 80rpx;
		background-color: #f9f9f9;
		line-height: 80rpx; // 垂直居中
		margin: 0 auto; // 水平居中
		border-radius: 80rpx;
		
		display: flex;
		
		.left{
			width: 140rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			// 修改uni-icons组件的颜色
			:deep(){
				.uni-icons{
					color: $brand-theme-color !important;
				}
			}
			.text{
				color: $brand-theme-color;
				font-weight: 600;
				font-size: 28rpx;
			}
		}
		.center{
			flex: 1;
			
			.my-swiper{
				height: 100%; 
				
				.my-swiper-item{
					height: 100%;
					font-size: 30rpx;
					color: #666;
					// 当文字过多时，展示...
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
				}
			}
		}
		.right{
			width: 70rpx;
		}
	}

	// 每日推荐
	.select{
		padding-top: 50rpx;
		
		.date{
			color: $brand-theme-color;
			display: flex;
			align-items: center;
			// 修改uni-icons组件的颜色
			:deep(){
				.uni-icons{
					color: $brand-theme-color !important;
				}
			}
			
			.text{
				margin-left: 5rpx;
			}
		}
		
		.content{
			width: 720rpx;
			margin-left: 30rpx;
			margin-top: 30rpx;
			
			scroll-view{
				white-space: nowrap; // 不换行
				
				.box{
					width: 200rpx;
					height: 430rpx;
					display: inline-block;
					margin-right: 15rpx;
					
					&:last-child{
						margin-right: 30rpx;
					}
					
					image{
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
				}
			}
		}
	}

	// 专题精选
	.theme{
		padding: 50rpx 0;
		
		.more{
			font-size: 32rpx;
			color: #888;
		}
		
		.content{
			margin-top: 30rpx;
			padding: 0 30rpx;
			display: grid;
			gap: 15rpx;
			grid-template-columns: repeat(3, 1fr);
		}
	}
	
}
</style>
